﻿/*
 * Copyright © 2011 Tristan Reeves, treeves@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using System;
using NUnit.Framework;
using Spring.Context.Support;
using Sprinkles.Secondary;
using Sprinkles.Tests.TestClasses;

namespace Sprinkles.Tests.UnitTests
{
    [TestFixture]
    public class CreationTests2
    {
        [Test]
        public void Can_Call_2_Arg_Constructor_with_object()
        {
            var ctx = new XmlApplicationContext(@"config\ConstructionConfig1.xml");
            var registeredObject = ctx["DummyHost2.constructor1.obj.2"] as ConstructorTest1;
            Assert.That(registeredObject, Is.Not.Null);
            Assert.That(registeredObject.X, Is.EqualTo(5));
            Assert.That(registeredObject.O.GetType() == typeof(Object));
        }

        [Test]
        public void Can_Call_2_Arg_Constructor_with_expression()
        {
            var ctx = new XmlApplicationContext(@"config\ConstructionConfig1.xml");
            var registeredObject = ctx["DummyHost2.constructor1.exp.2"] as ConstructorTest1;
            Assert.That(registeredObject, Is.Not.Null);
            Assert.That(registeredObject.X, Is.EqualTo(110));
            Assert.That(registeredObject.O, Is.EqualTo(22));
        }

        [Test]
        public void Can_Call_2_Arg_Constructor_with_placeholder()
        {
            var ctx = new XmlApplicationContext(@"config\ConstructionConfig1.xml");
            var registeredObject = ctx["DummyHost2.constructor1.ph.2"] as ConstructorTest1;

            Assert.That(registeredObject, Is.Not.Null);
            Assert.That(registeredObject.X, Is.EqualTo(88));
            Assert.That(registeredObject.O, Is.EqualTo("123123"));
        }

        [Test]
        public void Can_Call_2_Arg_Constructor_with_ref()
        {
            var ctx = new XmlApplicationContext(@"config\ConstructionConfig1.xml");
            var registeredObject = ctx["DummyHost2.constructor2.ref.2"] as ConstructorTest2;
            Assert.That(registeredObject, Is.Not.Null);
            Assert.That(registeredObject.CT1.X, Is.EqualTo(5));
            Assert.That(registeredObject.O, Is.EqualTo(ctx["DummyHost2.constructor1.obj.2"]));
        }

        [Test]
        public void scope_is_propagated([Values(Scope.Application, Scope.None, Scope.Request, Scope.Session, null)] Scope? scope, [Values(true, false)] bool singleton)
        {
            var singletonString = singleton ? "singleton" : "prototype";
            var scopeString = scope.HasValue ? scope.Value.ToSpringScope() : "scope-unset";
            var scopeString2 = (scope ?? Scope.None).ToSpringScope().Replace("none", "");
            if (scopeString2 == "") scopeString2 = singletonString;

            var nam = "DummyHost4." + scopeString + "." + singletonString;
            var ctx = new XmlApplicationContext(@"config\ConstructionConfig1.xml");

            var item = ctx.GetObject(nam);
            var definition = ctx.GetObjectDefinition(nam);
            Assert.That(item, Is.Not.Null);
            Assert.That(definition.Scope, Is.EqualTo(scopeString2));

            if ((scope != null) && (scope != Scope.None)) singleton = true;
            Assert.That(definition.IsSingleton, Is.EqualTo(singleton));
        }
    }
}
